package com.sws.worktive.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sws.worktive.DrawerFragment;
import com.sws.worktive.HomeFragment;
import com.sws.worktive.PostFragment;
import com.sws.worktive.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class DialogWindow extends DialogFragment implements OnClickListener {

	String txtTitle, selection;
	TextView txtheader;
	Button btnCamera, btnGallery;

	public static DialogWindow newInstance(String txtHeader, String selection) {
		DialogWindow window = new DialogWindow();

		Bundle arg = new Bundle();
		arg.putString("header", txtHeader);
		arg.putString("selection", selection);
		window.setArguments(arg);
		return window;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		txtTitle = getArguments().getString("header");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.dialog_window, container, false);
		// getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
		txtheader = (TextView) view.findViewById(R.id.txt_popHeader);
		txtheader.setText(txtTitle);

		btnCamera = (Button) view.findViewById(R.id.btn_camera);
		btnGallery = (Button) view.findViewById(R.id.btn_gallery);

		btnCamera.setOnClickListener(this);
		btnGallery.setOnClickListener(this);

		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.btn_camera:

			try {
				// Intent intentCamera = new Intent(
				// MediaStore.ACTION_IMAGE_CAPTURE);
				// File f = createImageFile();
				// intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,
				// Uri.fromFile(f));
				// ((HomeScreen) ((PostScreen) DialogWindow.this
				// .getParentFragment()).getParentFragment())
				// .getActivity().startActivityForResult(intentCamera,
				// Constant.CAMERA_REQUEST);
				((DrawerFragment) ((HomeFragment) ((PostFragment) DialogWindow.this
						.getParentFragment()).getParentFragment())
						.getActivity()).setCameraPreview();
				// .getActivity().startC
			} catch (Exception e) {
				// TODO: handle exception
			}

			DialogWindow.this.dismiss();
			break;

		case R.id.btn_gallery:

			Intent intentGallery = new Intent();
			intentGallery.setType("image/*");
			intentGallery.setAction(Intent.ACTION_GET_CONTENT);

			((HomeFragment) ((PostFragment) DialogWindow.this.getParentFragment())
					.getParentFragment()).getActivity().startActivityForResult(
					intentGallery, Constant.GALLERY_REQUEST);
			DialogWindow.this.dismiss();
			break;

		default:
			break;
		}

	}

	private File createImageFile() throws IOException {
		// Create an image file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		String imageFileName = timeStamp + "_";
		File image = File.createTempFile(imageFileName, ".JPEG", getAlbumDir());
		String mCurrentPhotoPath = image.getAbsolutePath();

		((DrawerFragment) ((HomeFragment) ((PostFragment) DialogWindow.this
				.getParentFragment()).getParentFragment()).getActivity())
				.setCameraImagePath(image.getAbsolutePath());
		return image;
	}

	private File getAlbumDir() {
		File file = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
				"WorkTive");
		file.mkdirs();
		return file;

	}

}
